package com.ctgu.chatapp.view.adapter

import android.annotation.SuppressLint
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import com.ctgu.chatapp.R
import java.text.SimpleDateFormat
import java.util.*

object BindingAdapter {

    @SuppressLint("SimpleDateFormat")
    private val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm")

    @JvmStatic
    @BindingAdapter("android:transformationMethod")
    fun setTransformationMethod(view: EditText, boolean: MutableLiveData<Boolean>) {
        when (boolean.value) {
            true -> view.transformationMethod = PasswordTransformationMethod.getInstance()
            false -> view.transformationMethod = HideReturnsTransformationMethod.getInstance()
            else -> view.transformationMethod =
                PasswordTransformationMethod.getInstance()
        }
    }

    @JvmStatic
    @BindingAdapter("regex", "str")
    fun checkRegex(text: TextView, regex: String, string: MutableLiveData<String>) {
        if (string.value?.let { Regex(regex).matches(it) } == true) text.visibility = View.INVISIBLE
        else text.visibility = View.VISIBLE

    }

    @JvmStatic
    @BindingAdapter("imgUrl")
    fun setImage(view: ImageView, url: String) {
        if (url == "default") {
            view.load(R.drawable.defaul) {
                transformations(CircleCropTransformation())
            }
        } else {
            view.load(url) {
                crossfade(true)
                crossfade(200)
                transformations(CircleCropTransformation())
            }
        }
    }

    @JvmStatic
    @BindingAdapter("LinearLayout")
    fun setAdapter(recyclerView: RecyclerView, boolean: Boolean) {
        recyclerView.layoutManager =
            LinearLayoutManager(recyclerView.context, LinearLayoutManager.VERTICAL, boolean)
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    @JvmStatic
    @BindingAdapter("Sex")
    fun setSex(imageView: ImageView, int: Int) {
        when (int) {
            0 -> imageView.background = imageView.resources.getDrawable(R.drawable.male, null)
            1 -> imageView.background = imageView.resources.getDrawable(R.drawable.female, null)
            else -> imageView.background =
                imageView.resources.getDrawable(R.drawable.ic_doublesex, null)
        }
    }

    @JvmStatic
    @BindingAdapter("State")
    fun setStateTextView(textView: TextView, int: Int) {
        when (int) {
            0 -> textView.text = "等待验证"
            1 -> textView.text = "已通过"
            2 -> textView.text = "已拒绝"
            else -> textView.visibility = View.GONE
        }
    }

    @JvmStatic
    @BindingAdapter("State")
    fun setStateButton(button: Button, int: Int) {
        when (int) {
            3, 4, 5 -> button.visibility = View.VISIBLE
            else -> button.visibility = View.GONE
        }
    }

    @JvmStatic
    @BindingAdapter("Date")
    fun setDate(textView: TextView, timestamp: Long) {
        textView.text = simpleDateFormat.format(Date(timestamp))
    }
}